package com.hxc.controller;

import com.github.pagehelper.PageInfo;
import com.hxc.pojo.User;
import com.hxc.service.UserService;
import com.hxc.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("list")
    public String list(UserVo userVo, @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        PageInfo<User> userPageInfo = userService.listAll(userVo, pageNum, pageSize);
        model.addAttribute("pg",userPageInfo);
        model.addAttribute("userVo",userVo);
        return "list";
    }

    @ResponseBody
    @RequestMapping("delUser")
    public Boolean delUser(@RequestParam("ids[]") Integer[] ids){
        return userService.delUser(ids)>0;
    }

    @RequestMapping("getUser")
    public String getUser(Integer id,Model model){
        User user = userService.getUser(id);
        model.addAttribute("user",user);
        return "updateUser";
    }

    @ResponseBody
    @RequestMapping("updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:list";
    }
}
